package bzk3.geometry;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

//import android.opengl.GLES20;
import br.odb.libscene.Material;
import br.odb.libstrip.AbstractTriangle;
import br.odb.libstrip.IndexedSetFace;
import br.odb.utils.math.Vec3;

/**
 * 
 * @author monty
 *
 */
public class GLES1Triangle implements GLESIndexedSetFace, AbstractTriangle {

	public float a;
	public float b;
	private float[] color = new float[12];
	private FloatBuffer colorBuffer;
	public float g;
	public float r;
	private FloatBuffer vertexBuffer;
	private float vertices[] = new float[9];
	private boolean visible = true;
	public float x0;
	public float x1;
	public float x2;
	public float y0;
	public float y1;
	public float y2;
	public float z0;
	public float z1;
	public float z2;
	// ------------------------------------------------------------------------------------------------------------
	/**
	 * 
	 */
	@Override
	public void addIndex(int index) {
	}
	// ------------------------------------------------------------------------------------------------------------
	/**
	 * 
	 */
	@Override
	public void addVertex(Vec3 v) {
		// TODO Auto-generated method stub

	}
	// ------------------------------------------------------------------------------------------------------------
	/**
	 * 
	 */
	@Override
	public void draw(GL10 gl) {

		if ( visible ) {
			
			gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer);
			gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
			gl.glDrawArrays(GL10.GL_TRIANGLES, 0, vertices.length / 3);
		}
	}
	// ------------------------------------------------------------------------------------------------------------
	/**
	 * 
	 */
	public void flatten() {
		z0 = z1 = z2 = -1.2f;
	}
	// ------------------------------------------------------------------------------------------------------------
	/**
	 * 
	 */
	public void flushToGLES() {
		
		vertices[0] = x0;
		vertices[1] = y0;
		vertices[2] = z0;
		
		vertices[3] = x1;
		vertices[4] = y1;
		vertices[5] = z1;
		
		vertices[6] = x2;
		vertices[7] = y2;
		vertices[8] = z2;
		
		color[0] = r / 255.0f;
		color[1] = g / 255.0f;
		color[2] = b / 255.0f;
		color[3] = a / 255.0f;

		color[4] = r / 255.0f;
		color[5] = g / 255.0f;
		color[6] = b / 255.0f;
		color[7] = a / 255.0f;

		color[8] = r / 255.0f;
		color[9] = g / 255.0f;
		color[10] = b / 255.0f;
		color[11] = a / 255.0f;

		ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4);
		byteBuf.order(ByteOrder.nativeOrder());
		vertexBuffer = byteBuf.asFloatBuffer();
		vertexBuffer.put(vertices);
		vertexBuffer.position(0);

		byteBuf = ByteBuffer.allocateDirect(color.length * 4);
		byteBuf.order(ByteOrder.nativeOrder());
		colorBuffer = byteBuf.asFloatBuffer();
		colorBuffer.put(color);
		colorBuffer.position(0);

	}
	// ------------------------------------------------------------------------------------------------------------
	/**
	 * 
	 */
	@Override
	public br.odb.libscene.Color getColor() {

		return new br.odb.libscene.Color( color[ 0 ], color[ 1 ], color[ 2 ], color[ 3 ] );
	}
	// ------------------------------------------------------------------------------------------------------------
	/**
	 * 
	 */
	@Override
	public float getColor(int i) {
		return color[i];
	}
	// ------------------------------------------------------------------------------------------------------------
	/**
	 * 
	 */
	@Override
	public float[] getColorData() {
		return color;
	}
	// ------------------------------------------------------------------------------------------------------------
	/**
	 * 
	 */
	@Override
	public int getIndex(int d) {

		return d;
	}
	// ------------------------------------------------------------------------------------------------------------
	/**
	 * 
	 */
	@Override
	public int getTotalIndexes() {

		return 3;
	}
	// ------------------------------------------------------------------------------------------------------------
	/**
	 * 
	 */
	@Override
	public Vec3 getVertex(int c) {
		// TODO Auto-generated method stub
		return null;
	}
	// ------------------------------------------------------------------------------------------------------------
	/**
	 * 
	 */
	@Override
	public float[] getVertexData() {
		return vertices;
	}
	// ------------------------------------------------------------------------------------------------------------
	/**
	 * 
	 */
	@Override
	public boolean getVisibility() {

		return visible;
	}
	// ------------------------------------------------------------------------------------------------------------
	/**
	 * 
	 */
	@Override
	public IndexedSetFace makeCopy() {
		GLES1Triangle t = new GLES1Triangle();
		t.a = a;
		t.b = b;
		t.g = g;
		t.r = r;

		t.visible = visible;
		t.x0 = x0;
		t.x1 = x1;
		t.x2 = x2;

		t.y0 = y0;
		t.y1 = y1;
		t.y2 = y2;

		t.z0 = z0;
		t.z1 = z1;
		t.z2 = z2;

		t.flushToGLES();

		return t;
	}
	// ------------------------------------------------------------------------------------------------------------
	/**
	 * 
	 */
	@Override
	public void setColor(br.odb.libscene.Color c) {
		this.color[0] = c.getA();
		this.color[1] = c.getR();
		this.color[2] = c.getG();
		this.color[3] = c.getB();

	}
	// ------------------------------------------------------------------------------------------------------------
	// public void drawGLES2(int vertexHandle, int colorHandle ) {
	// GLES20.glVertexAttribPointer( vertexHandle, vertices.length / 3,
	// GLES20.GL_FLOAT, false, 0, vertexBuffer );
	// GLES20.glEnableVertexAttribArray( vertexHandle );
	//
	// GLES20.glVertexAttribPointer( colorHandle, 4, GLES20.GL_FLOAT, false, 0,
	// colorBuffer );
	// GLES20.glEnableVertexAttribArray( colorHandle );
	//
	// GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 3 );
	// }
	// ------------------------------------------------------------------------------------------------------------
	/**
	 * 
	 */
	@Override
	public void setVisibility(boolean visibility) {

		visible = visibility;
	}
	@Override
	public Material getMaterial() {
		// TODO Auto-generated method stub
		return null;
	}
}
